package cn.fzkj.framework.security.handle;

import cn.fzkj.common.ResultVO;
import cn.fzkj.common.utils.ServletUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.google.gson.Gson;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;

/**
 * 认证失败处理类 返回未授权
 * 这是处理认证失败的，即处理一些用户名密码错误之类的【认证】没通过的，跟鉴权没关系，
 * 【鉴权】是登录之后拿着token访问权限不足的情况
 *
 * @author ruoyi
 */
@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint, Serializable {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
        String msg = "请求访问："+request.getRequestURI()+"认证失败，无法访问系统资源";
        ServletUtils.renderString(response, new Gson().toJson(ResultVO.failure(msg)));
    }
}
